package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Transform;
import com.codename1.ui.geom.AffineTransform;
import java.util.Arrays;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class AffineTransformTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.getContentPane().getStyle().setBgPainter((g, rect) -> {
            AffineTransform t = new AffineTransform();
            t.setToRotation(Math.PI/4, rect.getX() + rect.getWidth()/2, rect.getY() + rect.getHeight()/2);
            Transform tt = t.toTransform();
            System.out.println("Transform is "+tt);
            float[] res = tt.transformPoint(new float[]{rect.getX() + rect.getWidth()/2, rect.getY()});
            System.out.println("t(["+(rect.getX()+rect.getWidth()/2)+", "+rect.getY()+"]) = "+Arrays.toString(res));
            
            Transform rot = Transform.makeRotation((float)Math.PI/4, rect.getX() + rect.getWidth()/2, rect.getY() + rect.getHeight()/2);
            System.out.println("rot="+rot);
            g.setColor(0xff0000);
            g.setTransform(t.toTransform());
            g.fillRect(rect.getX() + rect.getWidth()/2-100, rect.getY() + rect.getHeight()/2 - 100, 200, 200);
            g.resetAffine();
        });
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
